#include "StdAfx.h"
#include "ProcessMgr.h"
#include "dao/procVoDao.h"
#include "dao/DAOFactory.h"
#include <algorithm>    
#include <vector>  


CProcessMgr::CProcessMgr(void):m_bLoad(false)
{

	m_pProcVoDao =  CDAOFactory::GetProcVoDao();
}


CProcessMgr::~CProcessMgr(void)
{
	if(m_pProcVoDao)
		delete m_pProcVoDao;
}





bool   CProcessMgr::AddAProc(CProcVo*  procVo)
{
	bool bOk;
	if(procVo == NULL)
		return false;

	do 
	{
		if(!m_pProcVoDao->AddAProc(*procVo))
		{
			delete procVo;
			bOk= false;
			break;
		}
		m_procVoVector.push_back(procVo);
		bOk = true;

	} while (false);
	

	return bOk;
	
}

bool   CProcessMgr::IsExistByProcName(CString& procName)
{
	bool  bRet = false;
	ProcessFinder  finder;
	finder.name = procName;
	vector<CProcVo*>::iterator  it;

	it = std::find_if(m_procVoVector.begin(),m_procVoVector.end(),finder);

	if (it != m_procVoVector.end())
	{
		return true;
	}
	return false;
	
}



bool   CProcessMgr::DelAProc(CProcVo*  procVo)
{
	if(procVo == NULL)
		return false;

	bool bOk= false;

	do 
	{

	
		vector<CProcVo*>::iterator  it;
		if(!m_pProcVoDao->DelAProc(procVo->m_ProcessName))
		{
			break;
		}
		for(it = m_procVoVector.begin();it != m_procVoVector.end();it++)
		{
			if((*it )== procVo )
			{
				m_procVoVector.erase(it);
		
				break;
			}
		}
		delete procVo;
		bOk = true;

	} while (FALSE);

	return bOk;


}


void    CProcessMgr::LoadProcInfo()
{
	CProcVo      *procVo;

	if(m_bLoad)
		return;

	do 
	{
		if(!m_pProcVoDao->GetAllProc())
			break;

		procVo = new CProcVo;
		while(m_pProcVoDao->EnumProcInfo(*procVo))
		{
			m_procVoVector.push_back(procVo);
			procVo = new CProcVo;
		}

		if(procVo)
			delete procVo;

	} while (FALSE);

	m_bLoad = true;

	return ;
}



void    CProcessMgr::Clear()
{

	CProcVo*  pProc = NULL;
	while(m_procVoVector.size())
	{
		pProc = m_procVoVector.back();
		m_procVoVector.pop_back();
		if(pProc)
			delete  pProc;
	}
	m_bLoad = false;


}